# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'MainWindowUI.ui'
##
## Created by: Qt User Interface Compiler version 6.1.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from UI.ReturnAssetDialog import ReturnAssetDialog
from datetime import datetime
from Infrastructure.AssetStatus import AssetStatus
from UI.WarehousingInDialog import WarehousingInDialog
from UI.RecAssetDialog import RecAssetDialog
from PySide6.QtCore import *  # type: ignore
from PySide6.QtGui import *  # type: ignore
from PySide6.QtWidgets import *  # type: ignore
from uuid import uuid1
from MainWindowUI import Ui_MainWindow
from ElectronicAssetDAO import ElectronicAssetDAO
from OrmBase import Base, engine


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        Base.metadata.create_all(engine)
        self.setupUi(self)
        self.receptionMenuAction.triggered.connect(self.button_reception)
        self.returnMenuAction.triggered.connect(self.button_return)
        self.warehousing_inMenuAction.triggered.connect(self.button_warehousing_in)
        self.destructionMenuAction.triggered.connect(self.button_destruction)

    def button_reception(self):
        print("领用")
        self.recAssetDialog = RecAssetDialog(self)
        result = self.recAssetDialog.exec()
        if result is 1:
            for rowIndex in range(self.recAssetDialog.recAssetsTable.rowCount()):
                arg = {
                    "status": AssetStatus.Allotted,
                    "user_id": self.recAssetDialog.recAssetsTable.cellWidget(rowIndex, 10).data.uuid,
                    "using_locate": self.recAssetDialog.recAssetsTable.item(rowIndex, 9).text(),
                }
                recAsset = ElectronicAssetDAO.update_device(
                    devices_uuid= self.recAssetDialog.recAssetsTable.cellWidget(rowIndex, 0).data.uuid,
                    **arg
                )
        self.assetManageTab.buttonRefreshClicked()

    def button_return(self):
        print("退回")
        returnAssetDialog = ReturnAssetDialog(self)
        result = returnAssetDialog.exec()
        if result is 1:
            for rowIndex in range(returnAssetDialog.returnAssetsTable.rowCount()):
                arg = {
                    "status": AssetStatus.InWarehouse,
                    "user_id": "",
                    "using_locate": "",
                }
                recAsset = ElectronicAssetDAO.update_device(
                    devices_uuid= returnAssetDialog.returnAssetsTable.cellWidget(rowIndex, 0).data.uuid,
                    **arg
                )
        self.assetManageTab.buttonRefreshClicked()

    def button_warehousing_in(self):
        print("入库")
        self.warehousingInDialog = WarehousingInDialog(self)
        result = self.warehousingInDialog.exec()
        if result is 1:
            for rowIndex in range(self.warehousingInDialog.wInAssetsTable.rowCount()):
                eAsset = ElectronicAssetDAO.add_device(**{
                    "uuid": str(uuid1()),
                    "asset_ID": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 0).text(),
                    "device_ID": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 1).text(),
                    "device_category": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 2).text(),
                    "brand": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 3).text(),
                    "device_model": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 4).text(),
                    "using_type": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 5).text(),
                    "origin": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 6).text(),
                    "status": AssetStatus.InWarehouse,
                    "application": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 7).text(),
                    "production_date": datetime.strptime(self.warehousingInDialog.wInAssetsTable.item(rowIndex, 8).text(), "%Y\%m\%d"),
                    "user_id": None,
                    "remarks": self.warehousingInDialog.wInAssetsTable.item(rowIndex, 9).text()
                })
        self.assetManageTab.buttonRefreshClicked()

    def button_destruction(self):
        print("销毁")